package com.uwlaser.service.impl;

import com.uwlaser.domain.ResultTable;
import com.uwlaser.service.ChartDataService;
import com.uwlaser.service.dto.*;
import com.uwlaser.service.mapper.ChartSourceMapper;
import com.uwlaser.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: UW_13724@caoguilin
 * @time: 2022/12/29 16:59
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ChartDataServiceImpl implements ChartDataService {

    private final ChartSourceMapper chartSourceMapper;
    @Override
    public ResultTable getOutput(GetChartDataSourceParam requestParam) {
        ResultTable resultTable=new ResultTable();
        //获取参数

        String year = requestParam.getYear();
        String month = requestParam.getMonth();
        if(year == null || month== null || year == "" || month == ""){
            resultTable.setCode(400);
            resultTable.setMsg("年/月参数值为空!");
            return resultTable;
        }
        DataMainQueryParam queryParam=new DataMainQueryParam();
        queryParam.setYear(year);
        queryParam.setMonth(month);

        //根据年、月查询产量
        List<DataMainDto> dataMainDtos = chartSourceMapper.selectDailyOutput(queryParam);
        //迭代数据集合，封装成前端可解析的格式
        List<Source> list=new ArrayList<>();
        try {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM");
            Date date = dateFormat.parse(year+"-"+month);
            calendar.setTime(date);
            // 获取这个月的天数
            int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        for(int i=1;i<=days;i++){
            if(dataMainDtos.size()>0){
                for(int j=0;j<dataMainDtos.size();j++){
                    Source source=new Source();
                    // 如果当日没有那么就赋值为0
                    if(Integer.parseInt(dataMainDtos.get(j).getDay())==i){
                        source.setDay(DateUtil.getDateFormat(dataMainDtos.get(j).getEndTime(),"yyyy/MM/dd"));
                        source.setValue(dataMainDtos.get(j).getDailyOutput());
                        list.add(source);
                        //将已经赋值的元素删除
                        dataMainDtos.remove(j);
                        break;
                    }else {
                        SimpleDateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
                        Date date2 = dateFormat2.parse(year+"-"+month+"-"+i);
                        source.setDay(DateUtil.getDateFormat(date2,"yyyy/MM/dd"));
                        source.setValue(0);
                        list.add(source);
                        break;
                    }
                }
            }else {
                Source source=new Source();
                SimpleDateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
                Date date2 = dateFormat2.parse(year+"-"+month+"-"+i);
                source.setDay(DateUtil.getDateFormat(date2,"yyyy/MM/dd"));
                source.setValue(0);
                list.add(source);
            }

        }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        ChartDataSource dataSource=new ChartDataSource();
        List<String> dimensions= new ArrayList<>();
        dimensions.add("day");
        dimensions.add("value");
        // 封装返回数据
        dataSource.setDimensions(dimensions);
        dataSource.setSource(list);
        // 添加返回数据
        resultTable.setData(dataSource);
        resultTable.setCode(200);
        resultTable.setMsg("获取成功!");
        return resultTable;
    }

    @Override
    public ResultTable getYearOutput(GetChartDataSourceParam requestParam) {
        ResultTable resultTable = new ResultTable();
        //获取参数
        String year = requestParam.getYear();
        if(year == null || year == ""){
            resultTable.setCode(400);
            resultTable.setMsg("年份参数值为空!");
            return resultTable;
        }
        DataMainQueryParam queryParam=new DataMainQueryParam();
        queryParam.setYear(year);
        //根据年查询月产量
        List<DataMainDto> dataMainDtos = chartSourceMapper.selectYearOutput(queryParam);
        //迭代数据集合，封装成前端可解析的格式
        List<Source> list=new ArrayList<>();
        for (DataMainDto dataMainDto : dataMainDtos) {
            Source source=new Source();
            source.setDay(DateUtil.getDateFormat(dataMainDto.getEndTime(),"yyyy/MM"));
            source.setData1(dataMainDto.getMonthOutput());
            source.setData2(dataMainDto.getOKMonthOutput());
            list.add(source);
        }
        ChartDataSource dataSource=new ChartDataSource();
        List<String> dimensions= new ArrayList<>();
        dimensions.add("day");
        dimensions.add("data1");
        dimensions.add("data2");
        // 封装返回对象
        dataSource.setDimensions(dimensions);
        dataSource.setSource(list);
        // 添加返回数据
        resultTable.setData(dataSource);
        resultTable.setCode(200);
        resultTable.setMsg("获取成功!");
        return resultTable;
    }

    @Override
    public ResultTable getPassRate(GetChartDataSourceParam requestParam) {
        ResultTable resultTable = new ResultTable();
        //获取参数
        String year = requestParam.getYear();
        String month = requestParam.getMonth();
        if(year == null ||  year == ""){
            resultTable.setCode(400);
            resultTable.setMsg("年份参数值为空!");
            return resultTable;
        }
        DataMainQueryParam queryParam=new DataMainQueryParam();
        queryParam.setYear(year);
        queryParam.setMonth(month);
        //根据年查询月产量
        DataMainDto dataMainDto = chartSourceMapper.selectPassRate(queryParam);
        //迭代数据集合，封装成前端可解析的格式
        List<Source> list=new ArrayList<>();
        Source OKSource = new Source();
        OKSource.setProduct("OK");
        OKSource.setData1(dataMainDto.getOKRate());
        list.add(OKSource);
        Source NGSource = new Source();
        NGSource.setProduct("NG");
        NGSource.setData1(dataMainDto.getNGRate());
        list.add(NGSource);
        ChartDataSource dataSource=new ChartDataSource();
        List<String> dimensions= new ArrayList<>();
        dimensions.add("product");
        dimensions.add("data1");
        dataSource.setDimensions(dimensions);
        dataSource.setSource(list);
        // 添加返回数据
        resultTable.setData(dataSource);
        resultTable.setCode(200);
        resultTable.setMsg("获取成功!");

        return resultTable;
    }

    @Override
    public ResultTable getTodayOutput() {
        ResultTable resultTable=new ResultTable();
        resultTable.setData(chartSourceMapper.selectTodayOutput());
        resultTable.setCode(200);
        resultTable.setMsg("获取成功！");
        return resultTable;
    }

    @Override
    public ResultTable getWorkOrder(GetChartDataSourceParam requestParam) {
        ResultTable resultTable=new ResultTable();
        resultTable.setData(chartSourceMapper.selectProductionWorkOrder(requestParam.getProductType()));
        resultTable.setCode(200);
        resultTable.setMsg("获取成功！");
        return resultTable;
    }

    @Override
    public ResultTable getProductCode(GetChartDataSourceParam param) {
        ResultTable resultTable=new ResultTable();
        resultTable.setData(chartSourceMapper.selectProductCode(param.getProductType()));
        resultTable.setCode(200);
        resultTable.setMsg("获取成功！");
        return resultTable;
    }

    @Override
    public ResultTable getCellCapacity(GetChartDataSourceParam param) {
        ResultTable resultTable=new ResultTable();
        resultTable.setData(chartSourceMapper.selectCellCapacity(param.getProductCode()));
        resultTable.setCode(200);
        resultTable.setMsg("获取成功！");
        return resultTable;
    }

    @Override
    public ResultTable queryDataBySQL(String  param) {
        ResultTable resultTable=new ResultTable();
        resultTable.setData(chartSourceMapper.selectDataBySQL(param));
        resultTable.setCode(200);
        resultTable.setMsg("获取成功！");
        return resultTable;
    }

    @Override
    public ResultTable queryBatchOutput(GetChartDataSourceParam param) {
        ResultTable resultTable = new ResultTable();
        //获取天数
        String num = param.getNum();
        String productType = param.getProductType();
        DataMainQueryParam queryParam=new DataMainQueryParam();
        queryParam.setNum(num);
        queryParam.setProductType(productType);
        //根据天数查询产量
        List<DataMainDto> dataMainDtos = chartSourceMapper.selectBatchCount(queryParam);
        //迭代数据集合，封装成前端可解析的格式
        List<Source> list=new ArrayList<>();
        for (DataMainDto dataMainDto : dataMainDtos) {
            Source source=new Source();
            source.setDay(DateUtil.getDateFormat(dataMainDto.getEndTime(),"MM/dd"));
            source.setOk(dataMainDto.getOKCount());
            source.setNg(dataMainDto.getNGCount());
            list.add(source);
        }
        ChartDataSource dataSource=new ChartDataSource();
        List<String> dimensions= new ArrayList<>();
        dimensions.add("day");
        dimensions.add("ok");
        dimensions.add("ng");
        // 封装返回对象
        dataSource.setDimensions(dimensions);
        dataSource.setSource(list);
        // 添加返回数据
        resultTable.setData(dataSource);
        resultTable.setCode(200);
        resultTable.setMsg("获取成功!");
        return resultTable;
    }

    @Override
    public ResultTable queryBatchRate(GetChartDataSourceParam param) {
        ResultTable resultTable = new ResultTable();
        //获取天数
        String num = param.getNum();
        String productType = param.getProductType();
        DataMainQueryParam queryParam=new DataMainQueryParam();
        queryParam.setNum(num);
        queryParam.setProductType(productType);
        //根据天数查询合格率
        List<DataMainDto> dataMainDtos = chartSourceMapper.selectBatchRate(queryParam);
        //迭代数据集合，封装成前端可解析的格式
        List<Source> list=new ArrayList<>();
        for (DataMainDto dataMainDto : dataMainDtos) {
            Source source=new Source();
            source.setDay(DateUtil.getDateFormat(dataMainDto.getEndTime(),"MM/dd"));
            source.setOk(dataMainDto.getOKRate());
            source.setNg(dataMainDto.getNGRate());
            list.add(source);
        }
        ChartDataSource dataSource=new ChartDataSource();
        List<String> dimensions= new ArrayList<>();
        dimensions.add("day");
        dimensions.add("ok");
        dimensions.add("ng");
        // 封装返回对象
        dataSource.setDimensions(dimensions);
        dataSource.setSource(list);
        // 添加返回数据
        resultTable.setData(dataSource);
        resultTable.setCode(200);
        resultTable.setMsg("获取成功!");
        return resultTable;
    }
}
